﻿using System;
using System.Collections.Generic;
using RestSharp;
namespace CoinsLibrary
{
    public static class Http
    {
        public static string HttpGet(string url, string resource, Dictionary<string, string> kvs = null, Dictionary<string, string> headers = null)
        {
            var client = new RestClient(url);
            var request = new RestRequest(resource, Method.GET);
            request.Timeout = 3000;
            if (kvs != null)
            {
                foreach (var _kv in kvs)
                {
                    request.AddParameter(_kv.Key, _kv.Value);
                }
            }
            if (headers != null)
            {
                foreach (var _kv in headers)
                {
                    request.AddHeader(_kv.Key, _kv.Value);
                }
            }
            request.AddHeader("User-Agent", @"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36");
            IRestResponse response = client.Execute(request);
            var content = response.Content;
            return content;
        }
        public static string HttpPost(string url, string resource, Dictionary<string, string> kvs = null, Dictionary<string, string> headers = null)
        {
            var client = new RestClient(url);
            var request = new RestRequest(resource, Method.POST);
            request.Timeout = 3000;
            if (kvs != null)
            {
                foreach (var _kv in kvs)
                {
                    request.AddParameter(_kv.Key, _kv.Value);
                }
            }
            if (headers != null)
            {
                foreach (var _kv in headers)
                {
                    request.AddHeader(_kv.Key, _kv.Value);
                }
            }
            request.AddHeader("User-Agent", @"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36");
            IRestResponse response = client.Execute(request);
            var content = response.Content;
            return content;
        }

        public static string HttpPost(string url, string resource, string postdata, Dictionary<string, string> headers)
        {
            var client = new RestClient(url);
            var request = new RestRequest(resource, Method.POST);
            request.Timeout = 3000;
            //request.AddHeader("Accept", "application/json");
            foreach (var _kv in headers)
            {
                request.AddHeader(_kv.Key, _kv.Value);
            }
            request.AddParameter("application/json", postdata, ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);
            var content = response.Content;
            return content;
        }

    }
}
